package com.dba.example.guice.beans;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class OrderBy {
	
	public static Direction DEFAULT_DIRECTION = Direction.ASC;
	
	public static enum Direction {
		ASC, DESC;
	}

	private String property;

	private Direction direction;
	
	public OrderBy(String property) {
		this(property, DEFAULT_DIRECTION);
	}

	public OrderBy(String property, Direction direction) {
		this.property = property;
		this.direction = direction;
	}

	public OrderBy(String property, String direction) {
		this.property = property;
		try {
			this.direction = Direction.valueOf(direction.toUpperCase());
		} catch (Exception e) {
			this.direction = DEFAULT_DIRECTION;
		}
	}

	public String getProperty() {
		return property;
	}

	public Direction getDirection() {
		return direction;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((direction == null) ? 0 : direction.hashCode());
		result = prime * result
				+ ((property == null) ? 0 : property.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OrderBy other = (OrderBy) obj;
		if (direction != other.direction)
			return false;
		if (property == null) {
			if (other.property != null)
				return false;
		} else if (!property.equals(other.property))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "OrderBy [property=" + property + ", direction=" + direction
				+ "]";
	}

}
